package com.example.beikeyigou.login_reg;

import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.beikeyigou.R;
import com.example.beikeyigou.utils.BaseFragment;
import com.example.beikeyigou.utils.SaveUserUtils;
import com.example.beikeyigou.utils.URLUtils;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;

/**
 * 登录页面
 */
public class LoginFragment extends BaseFragment {
    EditText telEt, pwdEt;
    Button loginBtn;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_login, container, false);
        initView(view);
        return view;
    }

    private void initView(View view) {
        telEt = view.findViewById(R.id.frag_login_et_tel);
        pwdEt = view.findViewById(R.id.frag_login_et_password);
        loginBtn = view.findViewById(R.id.frag_login_btn);
        loginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String tel = telEt.getText().toString().trim();
                String pwd = pwdEt.getText().toString();

                if (judgeInput(tel, pwd)) {
                    Map<String, String> map = new HashMap<>();
                    map.put("phone", tel);
                    map.put("password", pwd);
                    postNetwork(URLUtils.login_url, map);
                }
            }
        });
    }

    @Override
    public void onSuccess(String result) {
        super.onSuccess(result);
        // 解析数据
        UserBean bean = new Gson().fromJson(result, UserBean.class);
        int code = bean.getCode();
        switch (code) {
            case 200:       // 登录成功
                Toast.makeText(getContext(), "登录成功", Toast.LENGTH_SHORT).show();
                // 保存登录信息
                UserBean.DataBean data = bean.getData();
                SaveUserUtils.saveUserToFile(getContext(), data);
                // 跳转到个人中心页面
                Intent intent = new Intent(getActivity(), SelfDataChangeActivity.class);
                startActivity(intent);
                getActivity().finish();
                break;
            case 201:       // 用户名或密码错误
                Toast.makeText(getContext(), "用户名或密码错误", Toast.LENGTH_SHORT).show();
                break;
            default:        // 其他，登录失败
                Toast.makeText(getContext(), "登录失败", Toast.LENGTH_SHORT).show();
                break;
        }
    }

    private boolean judgeInput(String tel, String pwd) {
        if (TextUtils.isEmpty(tel)||TextUtils.isEmpty(pwd)) {
            Toast.makeText(getContext(), "输入内容不能为空！", Toast.LENGTH_SHORT).show();
            return false;
        };
        //判断输入的号码是否符合要求，使用正则表达式
        String regex = "^1(?:3\\d|4[4-9]|5[0-35-9]|6[67]|7[013-8]|8\\d|9\\d)\\d{8}$";
        if (!tel.matches(regex)) {
            Toast.makeText(getContext(), "输入手机号码不合法！", Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }
}